package com.jsb.laundry.common.utils.jhh;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class TestJhh {
    public static void main(String[] args) {
        String withdrawal = withdrawal("6228480039149086678", "中国农业银行", "王宽", "18302147671", 1, "142724198902101256");
        System.out.println(withdrawal);
    }
    public static String withdrawal(String yhk,String yh,String xm,String sj,double q,String sfz){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("company_name","聚申宝(上海)企业管理有限公司");
        jsonObject.put("zone_id",40);
        jsonObject.put("batch_no",uuid);
//        JSONObject parse = JSONObject.parseObject(accounts);
//        JSONArray array = parse.getJSONArray("accounts");
        JSONArray accounts = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name",xm);
        jsonObject1.put("id_card",sfz);
        jsonObject1.put("phone",sj);
        jsonObject1.put("bank_name",yh);
        jsonObject1.put("bank_num",yhk);
        jsonObject1.put("account_amount",q);
        jsonObject1.put("account_no",UUID.randomUUID().toString().replaceAll("-", ""));
        accounts.add(jsonObject1);
//        //对于account进行编码
//        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts",accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/batchTransfer/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 实名
     * @param idCardNo
     * @param name
     * @param phone
     * @param bankNum
     * @param idCardFrontImg
     * @param idCardReverseImg
     * @return
     */
    public static String realName(String idCardNo,String name,String phone,String bankNum,String idCardFrontImg,String idCardReverseImg){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("id_card_no",idCardNo);
        jsonObject.put("name",name);
        jsonObject.put("phone",phone);
        jsonObject.put("bank_num",bankNum);
        jsonObject.put("id_card_front_img",idCardFrontImg);
        jsonObject.put("id_card_reverse_img",idCardReverseImg);
        JSONArray accounts = new JSONArray();

        //对于account进行编码
        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts", accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/real/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查看实名
     * @param idCardNo
     * @return
     */
    public static String viewRealName(String idCardNo){
        Map jsonObject = new HashMap();
        UUID uuid = UUID.randomUUID();
        jsonObject.put("aid",627);//代理商编号
        jsonObject.put("zone_id",40);
        jsonObject.put("id_card_no",idCardNo);
        JSONArray accounts = new JSONArray();

        //对于account进行编码
        BASE64Encoder base64Decoder = new BASE64Encoder();
        jsonObject.put("accounts", accounts);

        String encode = "";
        String encode1 = "";
        jsonObject = sortByKey(jsonObject, false);
        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        int count = 0;
        for (Map.Entry<String, Object> entry : entries) {
            encode1 = entry.getKey() + "=" + entry.getValue();
            if(count==0) {
                encode += encode1;
            }else{
                encode += "&"+encode1;
            }
            count++;
        }

        String md5 = Md5Utils.MD5(encode+"HPGLf0*22!LZl(f*0!wdderwt4Lff").toLowerCase();
        jsonObject.put("verify",md5);
        try {
            JSONObject jsonObject3 = HttpsUtil.doPost("https://api.lingcaiai.com/v1/open/realQuery/", jsonObject);
            System.out.println(jsonObject3);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }
}
